<template>
  <el-carousel height="60vh">
    <el-carousel-item v-for="item in carousel" :key="item">
      <h3 text="2xl">{{ item.label }}</h3>
      <img :src="item.img" :alt="item.label">
    </el-carousel-item>
  </el-carousel>

  <el-divider content-position="left"></el-divider>

  <!--  搜索图书  -->
  <card-book/>

  <el-divider content-position="left"></el-divider>

  <el-row :gutter="20" style="width: 100%">
    <el-col :span="12">
      <el-card>
        <template #header>
          <div class="card-header">
            <el-icon><StarFilled /></el-icon>
            <span> 本月热榜</span>
          </div>
        </template>
        <echarts-bar v-model:name="NameList" v-model:val="ValList"/>
      </el-card>
    </el-col>

    <el-col :span="12">
      <el-card @click="showSpark" style="height: 600px;">
        <template #header>
          <div class="card-header">
            <el-icon><StarFilled /></el-icon>
            <span> 智慧问答</span>
          </div>
        </template>
        <loader-spark/>
        <dialog-spark v-model:visible="SparkVisible" v-model:question="SparkQuestion"
                      v-model:answer="SparkAnswer" v-model:loader="SparkLoader"
                      :on-spark="onSpark"/>
      </el-card>
    </el-col>
  </el-row>
</template>

<script setup lang="ts">
import {Promotion, StarFilled} from '@element-plus/icons-vue'
import {getCurrentInstance, onMounted, ref} from "vue";
import api from "@/api";
import {ElMessage, ElMessageBox, FormInstance} from "element-plus";
import CommonSearch from "@/components/common/CommonSearch.vue";
import CommonBook from "@/components/common/CommonBook.vue";
import CommonPager from "@/components/common/CommonPager.vue";
import EchartsBar from "@/components/echarts/EchartsBar.vue";
import DialogSpark from "@/components/dialog/DialogSpark.vue";
import LoaderSpark from "@/components/loader/LoaderSpark.vue";
import CardBook from "@/components/card/CardBook.vue"
import {FindBooknoParams} from "@/api/bookno";
import img_01 from '@/assets/bg.jpg'
import img_02 from '@/assets/carousel/5.jpg'
import img_03 from '@/assets/carousel/24.jpg'
import img_04 from '@/assets/carousel/22.jpg'

onMounted(() => {
  onEcharts();
})

const {appContext} = getCurrentInstance()!

const carousel = ref([
    { img: img_01, label: '欢迎使用智阅图书云借还管理系统'}
  , { img: img_02, label: '学而不思则罔，思而不学则殆。 ——孔子'}
  , { img: img_03, label: '读万卷书，行万里路。 ——刘彝'}
  , { img: img_04, label: '为中华之崛起而读书。 ——周恩来'}
])

//echarts
const NameList = ref([]);
const ValList = ref([]);

function onEcharts() {
  api.borrow.findBorrowEcharts(true).then((res: any) => {
    ElMessage({message: res.message, type: res.success ? 'success' : 'error', grouping: true, showClose: true});
    for (let i = 0; i < res.data.books.length; i++) {
      NameList.value.push(res.data.books[i].name);
      ValList.value.push(res.data.books[i].value);
    }
  })
}

// spark
const SparkVisible = ref(false);
const SparkQuestion = ref("");
const SparkAnswer = ref([]);
const SparkLoader = ref(false);

function showSpark() {
  SparkAnswer.value[0] = {msg: "欢迎使用智阅图书云借还系统，我是智能机器人小智，有什么问题尽管问我吧！", type: 1};
  SparkVisible.value = true;
}

function onSpark(e) {
  if (SparkQuestion.value == '') {
    ElMessage.info({message: "请输入有效问题", grouping: true, showClose: true});
    return false;
  }
  SparkAnswer.value.push({msg: SparkQuestion.value, type: 0});
  const QuestionTemp = SparkQuestion.value;
  SparkQuestion.value = "";
  SparkLoader.value = true;
  api.spark.sendQuestion(QuestionTemp).then((res: any) => {
    SparkAnswer.value.push({msg: res, type: 1});
    if (res) {
      SparkLoader.value = false;
    }
  })
}
</script>

<style scoped>
.el-carousel__item h3 {
  color: #fff;
  width: 100%;
  background-color: transparent;
  font-size: 40px;
  line-height: 60vh;
  position: absolute;
  text-align: center;
}
</style>